package yxy.game.pm2.command.union.boss;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionBossMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.boss.UnionBossHandle;
import yxy.game.pm2.module.union.boss.UnionPlayerBoss;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟BOSS 联盟BOSS挑战挑战战斗开始 */
@MessageType(proto = Command.UnionBossChanllengeExecute, classtype = UnionBossMessage.UnionBossChanllengeExecuteRequest.class)
public class CMD_UnionBossChanllengeExecute extends Command<UnionBossMessage.UnionBossChanllengeExecuteRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionBossMessage.UnionBossChanllengeExecuteRequest request) {
		String playerCode = messageSource.getTag();

		UnionBossMessage.UnionBossChanllengeExecuteResponse.Builder response = UnionBossMessage.UnionBossChanllengeExecuteResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {

				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();
				UnionPlayerBoss playerBoss = unionPlayer.getBoss();

				// 判断次数
				if (playerBoss.times() <= 0) {
					if (playerBoss.buytimes() <= 0) {
						send(messageSource, response, Result.UnionBossTimesLack);// 没有次数了 你的挑战次数用完了
						return;
					}
					// 有购买次数 直接花钱挑战
					int costNumber = fomula.联盟boss每日购买花费元宝数.intValue(playerBoss.getBuytimes());
					Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNumber);
					try {
						new CurrencyHandle().cost(playerCode, CCReason.联盟BOSS, cost);
						playerBoss.setBuytimes(playerBoss.getBuytimes() + 1);
						unionPlayer.set();
					} catch (CurrencyNotEnoughException e) {
						send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
						return;
					}
				}

				// 处理挑战
				result = new UnionBossHandle().battleResponseExecute(unionPlayer, union, response);
				send(messageSource, response, result);

				union.active();
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionBossMessage.UnionBossChanllengeExecuteResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}